//
//  RHTabBarController.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/5.
//

#import "RHTabBarController.h"
#import "RHHomeViewController.h"
#import "RHDataViewController.h"
#import "RHMessageViewController.h"
#import "RHPlantViewController.h"
#import "RHPlantListViewController.h"
#import "RHMineViewController.h"
#import "RHLoginViewController.h"
#import "RHNavigationController.h"
#import "RHPlantViewController.h"
#import "RHTools.h"
#import "RHMeAssetContainVC.h"

@interface RHTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic ,strong) UIView *lineV;

@end

@implementation RHTabBarController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if (![UINavigationBar appearance].translucent) {
        self.navigationController.navigationBar.subviews[0].subviews[0].hidden = YES;
    }else{
        self.navigationController.navigationBar.subviews[0].subviews[1].hidden = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
     self.tabBar.tintColor = [UIColor blackColor];
    
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    //Normal
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
    //Selected
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];
    [UITabBar appearance].translucent = NO;
    self.delegate = self;
    [self setupVC];
    [self setSelectedIndex:0];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userRoleDidChange) name:UserRoleDidChangeNotification object:nil];
   
}
-(void)userRoleDidChange
{
    [self setupVC];
    [self setSelectedIndex:3];
}

- (void)setupVC{

    //视图数组
    NSArray* controllerArr = @[@"RHOverviewVC", @"RHMeAssetContainVC", @"RHServiceVC",@"RHMineViewController"];
    //标题数组
    NSArray* titleArr = @[@"首页".rh_localized,@"资产".rh_localized,@"服务".rh_localized,@"我的".rh_localized];
    NSArray* picNormalArr = @[@"kwt_tab_home",@"kwt_tab_list",@"kwt_tab_server",@"kwt_tab_mine"];
    NSArray* picSelectArr = @[@"kwt_tab_home_s",@"kwt_tab_list_s",@"kwt_tab_server_s",@"kwt_tab_mine_s"];
    NSMutableArray* array = [[NSMutableArray alloc] init];
    for(int i=0; i<picNormalArr.count; i++) {
        Class cl=NSClassFromString(controllerArr[i]);
        UIViewController* controller = [[cl alloc]init];
        RHNavigationController *nv = [[RHNavigationController alloc] initWithRootViewController:controller];
    
        controller.title = titleArr[i];
        nv.tabBarItem.image = [[UIImage imageNamed:picNormalArr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //设置选中时的图片
        nv.tabBarItem.selectedImage = [[UIImage imageNamed:picSelectArr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //设置选中时字体的颜色(也可更改字体大小)
        [nv.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kCOLOR_HEX(@"3F4443")} forState:UIControlStateNormal];
        [nv.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kCOLOR_HEX(@"#05C79D")} forState:UIControlStateSelected];
        
        [array addObject:nv];
    }
    //ios13以上系统push后再回来,item的颜色会变成蓝色
    self.tabBar.tintColor = kCOLOR_HEX(@"3F4443");
    //背景颜色
    self.tabBar.backgroundColor = kRGB(255, 255, 255);
    
    if (@available(iOS 13.0,*)) {
        UITabBarAppearance *appearance = [UITabBarAppearance new];
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName:kCOLOR_HEX(@"3F4443")};
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName:kCOLOR_HEX(@"05C79D")};
        [UITabBar appearance].standardAppearance = appearance;
    }
    self.viewControllers = array;
    
}

#pragma mark - UITabBarControllerDelegate Method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    if([self.selectedViewController isKindOfClass:[UINavigationController class]]){
        UINavigationController *nav = (UINavigationController*)self.selectedViewController;
        NSLog(@"----->当前选择的控制器:%@",nav.topViewController);
        if([nav.topViewController isKindOfClass:[RHPlantViewController class]]){
            RHPlantViewController *plantVC = (RHPlantViewController*)nav.topViewController;
            [plantVC resignSearchBarController];
        }
    }
    
    
    return YES;
}


- (void)dealloc{
    [kNotificationCenter removeObserver:self];
    NSLog(@"%s",__func__);
}


@end
